<?php
/*
 * @project CreativeFramework
 * @version <#RELEASE_VERSION#>
 * @license GNU Lesser General Public License
 * @author Jimmie Lin
 */
define("APP_TYPE", "frontend");
require_once("core/boot.php");

// Timer Hook...
$timers['analyse_mvc'] = microtime(true);
$Hooks->run_hooks("analyse_mvc_start");

/*
 * Which app are we including?
 */
if(isset($_GET['app'])) {
	$app = $IN->str_only($_GET['app']);
}
else{
	$app = "global";
}

define("APPLICATION", $app);

/*
 * Which Controller are we running?
 */
if(isset($_GET['controller'])) {
	$controller = $IN->str_only($_GET['controller']);
}
else{
	$controller = "welcome";
}

define("CONTROLLER", $controller);

/*
 * Do we have an action code?
 */
if(isset($_GET['act'])) {
	$act = $IN->str_only($_GET['act']);
}
else{
	$act = false;
}
$Hooks->run_hooks("analyse_mvc_end");
$timers['analyse_session'] = microtime(true);
$Hooks->run_hooks("analyse_session_start");

/*
 * Analyse our Session State
 */
if(isset($_SESSION["cf_uid"])) {
	// Get this user!
	$Users_model = $Models->Load("users_model", "global");
	$userop = $Users_model->retrieve("singleid", $_SESSION["cf_uid"]);
	if(!$userop){ // OK, user has been deleted, log him out and drop silently
		unset($_SESSION["cf_uid"]);
		header("Location: index.php");
		die();
	}
	if(!isset($_SESSION["cf_passhash"]) or $_SESSION["cf_passhash"] != $userop['password']) {
		// User is no good.
		unset($_SESSION["cf_uid"]);
		header("Location: index.php");
		die();
	}
	$Smarty->assign("userop", $userop);
	// Get its group.
	$Groups_model = $Models->Load("groups_model", "global");
	$gop = $Groups_model->retrieve("singleid", $userop['group']);
	if(!$gop) { // Same, drop
		unset($_SESSION["cf_uid"]);
		header("Location: index.php");
		die();
	}
	$Smarty->assign("gop", $gop);
	if(!IN_STRICT) $Smarty->assign("grouparray", $gop);
}

// Timer Hook...
$Hooks->run_hooks("analyse_session_end");
$timers['ui_start'] = microtime(true);
$Hooks->run_hooks("main_start");

// TODO: User Configured Style ID
define("STYLE", $Main->config_value("default_style"));
define("STYLE_TPL_PATH", SMARTY_TEMPLATES_DIR.DIRECTORY_SEPARATOR."style_".STYLE);
define("STYLE_TPL_PATH_RELATIVE", APP_TYPE."_styles/style_".STYLE);

// Assign these to Smarty
$Smarty->assign("style_tpl_path_relative", STYLE_TPL_PATH_RELATIVE);
$Smarty->assign("style_id", STYLE);
$UI->startStyleFile(STYLE);

if($Controllers->PreLoad($controller) !== false) {
	// Menu, it up
	if(!IN_AJAX) {
		$MenuArray = $Main->nav_array_generate();
		$UI->Style->navmenu($MenuArray);
		$UI->Style->header($Controllers->Controller->title, $Controllers->Controller->desc);
	}
	// FIXME: Input
	$Controllers->PreLoadedRun(array(), $act);
	$Smarty->assign("queries", $DB->Driver->querycount);
	// Timer Hook...
	$timers['shutdown'] = microtime(true);
	$Smarty->assign("timers", $timers);
	if(!IN_AJAX) $UI->Style->footer(array());
}
else {
	// Lets four-oh-four
	if(IN_AJAX){
		echo "404";
	}
	else{
		$MenuArray = $Main->nav_array_generate();
		$UI->Style->navmenu($MenuArray);
		$UI->Style->header("404 Error", "");
		$Smarty->Display(STYLE_TPL_PATH_RELATIVE."/404.tpl");
		$Smarty->assign("queries", $DB->Driver->querycount);
		$timers['shutdown'] = microtime(true);
		$Smarty->assign("timers", $timers);
		$UI->Style->footer(array());
	}
}
